import AJAX from "./AJAX.js";
export default class Register {
    userObj;
    form;
    ids;
    ajax;
    ipts;
    judgeInput;
    m;
    constructor() {
        this.form=document.querySelector("form");
        this.ipts=document.querySelectorAll("input")
        this.rbtn = document.getElementById("registerbtn");
        
        this.m=new Map();
        // this.ajax=new AJAX
        this.bindEvent();
    }


    bindEvent(){
        this.form.addEventListener("input", e => this.inputHandler(e));
        this.rbtn.addEventListener("click", e => this.clickHandler(e))
    }
   
    async inputHandler(e){
        if(this.ids) return;
        var value = await this.clearTime(e.target);
        if(this.judgeIpt(e.target.name,value)){
            this.m.set(e.target.name,value)
            console.log( e.target.parentElement);
            e.target.parentElement.className="box green";
        }else{
            this.m.delete(e.target.name)
            console.log( e.target.parentElement);

            e.target.parentElement.className="box red";
        }
    }
    clearTime(input){
        return new Promise((resolve,reject)=>{
          this.ids=setTimeout(()=>{
              clearTimeout(this.ids);
              this.ids=undefined;
              resolve(input.value);
            },500);
        })
    }
    judgeIpt(name,value){
        switch(name){
            case 'tel':
                // return /^1\d{10}$/.test(value);
            case "password":
                // return /^(?=\D+\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9~!@#$%^&*-_+=.,";'\\\|\/]{6,16}$/.test(value);
        }
        return true;
        console.log(m);
    }
    async  clickHandler(e){
        e.preventDefault();
       
        if(this.m.size<2)return;
        var arr=Array.from(this.m.values());
         console.log(arr);
         var res=await fetch("http://localhost:4022/signup",{method:"post",body:JSON.stringify(arr)}).then(res => res.json());
        if(res){
            console.log(res);
            console.log("注册成功");
            location.href="./signIn.html";
        }else{
            console.log("注册失败");
        }
    }
   




//    clickHandler(e){
//        this.register();
//    }
//     register() {
//         this.getData();
//         console.log(111);
//        
//         var res = fetch("http://localhost:4022/signUp", { method: "post" ,body:JSON.stringify(arr)} );
//         console.log(res);
//     }
//   
//     }




}
new Register();



      